// pages/Order/showTicketOrder/index.js
const common = require('../../../public.js')
const config = require('../../../config')
const utils = require('../utils.js')
Page({

    /**
     * 页面的初始数据
     */
    data: {
        actives: 0,
        pageNum: 1,
        status: '0', // ' '=全部 ，1、未付款，2、已付款，5、交易成功，6、交易关闭
        orderList: [],
        refreshing: false,
        isLoadingBusy: false,
        isFyApp: config.isFyApp,
        active: 4,
        storeId: 0,
        moduleType: 1,
        tabshow: false
    },

    onLoad: function (options) {
        const that = this
        if (options.status) {
            console.log(options)
            that.setData({
                status: options.status,
                actives: options.status
            })
            if (options.id) {
                this.data.storeId = options.id ? options.id : config.id
                this.data.moduleType = options.moduleType ? options.moduleType : config.type
                this.setData({
                    tabshow: true
                })
            }


        }

        wx.getStorage({
            key: 'user',
            success: function (res) {
                that.setData({
                    userInfo: res.data.user,
                    token: res.data.token,
                    openid: res.data.openid
                }, () => {
                    that.reloadData()
                })
            }
        })
    },
    onChangeTabbar(e) { //切换底部导航
        const index = e.detail.index
        const url = e.detail.url
        if (index != this.data.active) {
            utils.onChangeTabbar(url, this.data.storeId, this.data.moduleType)
        }
    },
    getOrderList(refresh = false) {
        const that = this
        // 当前正在加载或没有获取到用户信息，不再加载数据
        if (this.data.isLoadingBusy || !this.data.userInfo) {
            return
        }
        // 是否是在切换tab页，切换tab页不显示下拉刷新提示
        if (refresh) {
            this.setData({
                noMoreData: false,
                refreshing: true,
                isLoadingBusy: true
            })
        } else {
            if (this.data.noMoreData === true) {
                return
            }
            this.setData({
                isLoadingBusy: true,
                refreshing: false
            })
        }

        const pageNum = refresh ? 1 : this.data.pageNum + 1
        let tempJson = {
            userId: that.data.userInfo.id,
            token: that.data.token,
            pageNum: pageNum,
            pageSize: 10,
            status: that.data.status == 0 ? '' : that.data.status,
        }

        common.post('/ticketOrder/getMyTicketOrderList', tempJson, function (res) {
            if (res.status == 200) {
                // 更新页码
                that.data.pageNum = pageNum
                that.setData({
                    orderList: refresh ? res.data : [...that.data.orderList, ...res.data]
                })
                if (res.data === null || res.data.length === 0) {
                    that.setData({
                        noMoreData: true
                    })
                }
            } else {
                common.showRequestErrMsg(res)
            }
        },
            function () {
                that.setData({
                    isLoadingBusy: false
                })
                if (refresh) {
                    that.setData({
                        refreshing: false
                    })
                }
            })
    },

    watchEticket(e) {
        let {
            item
        } = e.currentTarget.dataset
        //  console.log(e.currentTarget.dataset)
        wx.navigateTo({
            url: '/pages/ScenicArea/ScenicAreaETicket/index?orderNo=' + item.orderNo,
        })
    },
    toOrderDetail(e) {
        let {
            item
        } = e.currentTarget.dataset
        wx.navigateTo({
            url: '/pages/ScenicArea/ScenicAreaOrderDetail/index?orderNo=' + item.orderNo,
        })
    },
    payOrder: common.throttleFunc(function (e) {
        const that = this
        console.log(e)
        let {
            item
        } = e.currentTarget.dataset

        let tempJson = {
            orderId: item.id,
            userId: that.data.userInfo.id,
            token: that.data.token,
            //   openid: that.data.openid
            xcxOpenid: that.data.openid
        }
        // common.wxPayRequest('/wxxcxPayForScenicTicket', tempJson, function (res) 
        common.wxPayRequest("/wxxcxPayScenicTicketForRegionWLByChinaUms", tempJson, function (res) {
            // if (res.status == 400) {
            //     common.showToast(res.msg)
            // }
            wx.requestPayment({
                'timeStamp': res.data.timeStamp,
                'nonceStr': res.data.nonceStr,
                'package': res.data.package,
                'signType': res.data.signType,
                'paySign': res.data.paySign,
                'success': function (res) {
                    common.showToast('支付成功', 'success')
                    that.reloadData()
                },
                'fail': function (res) {
                    if (res.errMsg === 'requestPayment:fail cancel') {
                    } else {
                        common.showToast(res.errMsg)
                    }
                }
            })
        })
    }, 1000),
    deleteClosedOrder(e) { // 删除已经关闭订单
        const that = this
        common.showAlertView('提示', '是否删除该订单？', function (res) {
            if (res) {
                let {
                    item
                } = e.currentTarget.dataset
                let tempJson = {
                    userId: that.data.userInfo.id,
                    token: that.data.token,
                    orderNo: item.orderNo
                }
                common.post('/ticketOrder/delTicketOrder', tempJson, function (res) {
                    if (res.status == 200) {
                        common.showToast('删除成功')
                        that.reloadData()
                    } else {
                        common.showRequestErrMsg(res)
                    }
                })
            }
        })
    },
    onChange(e) {
        this.data.status = e.detail.name
        this.reloadData(true)
    },
    reloadData() {
        this.setData({
            orderList: [],
        }, _ => {
            this.getOrderList(true)
        })
    },
    loadMore() {
        this.getOrderList(false)
    },
    onReachBottom() {
        this.getOrderList(false)
    },
    onShow() {
        this.setData({
            stopmuic: false
        })
    },
})